package VN.EDU.IMIC.DEVELOPER.VUVANLUANSWINGJTABLEJSON;

import java.awt.EventQueue;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;

import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
import com.microsoft.sqlserver.jdbc.SQLServerException;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;

public class bangthemdulieu extends JFrame {

	private JPanel contentPane;
	private JTextField txtMSinhVin;
	private JTextField txtHVTn;
	private JTextField txtPhone;
	private JTextField txtMark;
	private JTextField txtEmail;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					bangthemdulieu frame = new bangthemdulieu();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @throws SQLException 
	 */
	public bangthemdulieu() throws SQLException {
		setTitle("Thong tin sinh vien");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		txtMSinhVin = new JTextField();
		txtMSinhVin.setText("ID");
		txtMSinhVin.setBounds(115, 11, 86, 20);
		contentPane.add(txtMSinhVin);
		txtMSinhVin.setColumns(10);
		
		txtHVTn = new JTextField();
		txtHVTn.setText("Fullname");
		txtHVTn.setBounds(115, 42, 86, 20);
		contentPane.add(txtHVTn);
		txtHVTn.setColumns(10);
		
		txtPhone = new JTextField();
		txtPhone.setText("Phone");
		txtPhone.setBounds(305, 11, 86, 20);
		contentPane.add(txtPhone);
		txtPhone.setColumns(10);
		
		txtMark = new JTextField();
		txtMark.setText("Mark");
		txtMark.setBounds(305, 73, 86, 20);
		contentPane.add(txtMark);
		txtMark.setColumns(10);
		
		txtEmail = new JTextField();
		txtEmail.setText("Email");
		txtEmail.setBounds(305, 42, 86, 20);
		contentPane.add(txtEmail);
		txtEmail.setColumns(10);
		
		JButton btnNewButton = new JButton("Thêm Sinh Viên");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				StudentEntity objStudent = new StudentEntity();
				objStudent.setEmail(txtEmail.getText());
				objStudent.setMark(txtMark.getText());
				objStudent.setPhone(txtPhone.getText());
				objStudent.setID(txtMSinhVin.getText());
				objStudent.setFullname(txtHVTn.getText());
				//clear
				txtEmail.setText("");
				txtMark.setText("");
				txtPhone.setText("");
				txtMSinhVin.setText("");
				txtHVTn.setText("");
				
				//themdulieu
			///

				//1.khai bao doi tuong Connection
				Connection objconnection;
				//2.khai bao va khoi tao doi tuong SQL serverdatasource
				
				SQLServerDataSource objdatasource = new SQLServerDataSource();
				objdatasource.setServerName("192.168.88.104");
				objdatasource.setUser("hocvien");
				objdatasource.setPassword("nothing");
				objdatasource.setDatabaseName("Student");
				objdatasource.setPortNumber(1433);
				//3.mo ket noi
				try {
					objconnection = objdatasource.getConnection();
					// kiem tra thong tin ket noi
			try {
							if(!objconnection.isClosed()){
								CallableStatement objSt = null;
// khoi tao doi tuong statetame
								objSt = objconnection.prepareCall("{call Themstudent(?,?,?,?,?)}");	
      objSt.setString("ID", objStudent.getID());
objSt.setString("Fullname", objStudent.getFullname());
      objSt.setString("email", objStudent.getEmail());
       objSt.setString("phone", objStudent.getPhone());
objSt.setString("mark",objStudent.getMark());
							
								objSt.execute();}
else{System.out.println("Ket noi that bai");}
						} catch (SQLException e) {
							e.printStackTrace();
						}
				} catch (SQLServerException e) {
					System.out.println("erroor" + e.getMessage());
				}
				////
			}
		});
		btnNewButton.setBounds(10, 130, 140, 23);
		contentPane.add(btnNewButton);
		
		JLabel lblId = new JLabel("ID");
		lblId.setBounds(66, 14, 46, 14);
		contentPane.add(lblId);
		
		JLabel lblFullname = new JLabel("Fullname");
		lblFullname.setBounds(66, 45, 46, 14);
		contentPane.add(lblFullname);
		
		JLabel lblPhone = new JLabel("Phone");
		lblPhone.setBounds(249, 14, 46, 14);
		contentPane.add(lblPhone);
		
		JLabel lblEmail = new JLabel("Email");
		lblEmail.setBounds(249, 45, 46, 14);
		contentPane.add(lblEmail);
		
		JLabel lblMark = new JLabel("Mark");
		lblMark.setBounds(249, 76, 46, 14);
		contentPane.add(lblMark);
		
		JButton btnNewButton_1 = new JButton("Hiện thị danh sách");
		btnNewButton_1.setBounds(10, 164, 140, 23);
		contentPane.add(btnNewButton_1);
		
		JButton btnNewButton_2 = new JButton("Cập nhật");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		btnNewButton_2.setBounds(10, 198, 140, 23);
		contentPane.add(btnNewButton_2);
		
		JButton btnNewButton_3 = new JButton("Thêm danh sách");
		btnNewButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//them bang du lieu
				try {
					BangthemXML_JSON_SQL frame = new BangthemXML_JSON_SQL();
					frame.setVisible(true);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		
		btnNewButton_3.setBounds(249, 130, 142, 23);
		contentPane.add(btnNewButton_3);
	
}
}